#include <iostream>
#include<conio.h>
#include<stdlib.h>
#include<string.h>
using namespace std;
# define MAX_IN_LINE 60 // Hang so xac dinh so luong ki tu MAX tren 1 dong
void str_copy(char *s1,char *s2,int i,int j) // Copy xau tu ki tu thu i den j cua sau s2 vao xau s1.
 {
   int k;
   memset(s1,'\0',1000);
   for (k=i;k<j;k++) s1[k-i]=s2[k];
   }
int is_space(char ch)   // Kiem tra xem ki tu "ch" co phai la DAU_CACH, DAU_TAB hay DAU_XUONG_DONG hay ko.
 {
  if ((ch == ' ') || (ch == '\n') || (ch == '\t')) return 1;
   else return 0;
   }
char *del_excess_space(char *a) // Xoa cac DAU_CACH thua, chi de lai 1 DAU_CACH giua 2 tu
 {  
    char *b;
    b=(char*)malloc(100000);
    int i,count=0;
	int n=strlen(a);
	while(is_space(a[0])==1)
	 {  
	    count++;
		for (i=0;i<n-1;i++) a[i]=a[i+1];
		n--;
	 }
	while(is_space(a[n-1])==1)
	 {  
	    count++;
		n--;
	 } 
	for(i=0;i<n-1;i++)
		if((is_space(a[i])==1)&&(is_space(a[i+1])==1))
		 {  
		    count++;
			for(int j=i;j<n-1;j++) a[j]=a[j+1];
			n--;
			i--;
		 } 
	n=strlen(a);
	str_copy(b,a,0,n-count);
	return b;
 }
char *del_space(char *a) // Xoa toan bo DAU_CACH cua text
 {
    char *b;
    b=(char*)malloc(100000);
    int i,count=0;
	a=del_excess_space(a);
	int n=strlen(a);
		for(i=0;i<n-1;i++)
		if((is_space(a[i])==1))
		 {  
		    count++;
			for(int j=i;j<n-1;j++) a[j]=a[j+1];
			n--;
			i--;
		 }
	n=strlen(a);
	str_copy(b,a,0,n-count);
	return b;
	
}
void left_align(char *a,int k) // Can le trai
 {
  int n=strlen(a);
  cout <<a;
  for (int i=n;i<k;i++) cout<<' ';
  cout<<"\n";
  	}
void right_align(char *a, int k) // Can le phai
 {
 	for (int i=0;i<k-strlen(a);i++) cout<<' '; 
    cout <<a;
    cout <<"\n";
 }
void mid_align(char *a, int k) // Can giua
 {
 	int i=k-strlen(a);
 	for (int j=1;j<=(i/2);j++) cout <<' ';
 	cout <<a;
 	for (int j=1;j<=(i-(i/2));j++) cout <<' ';
 	cout <<"\n";
 }
int main(void) 
{ 
char *text,line[1000],path[100];
text=(char*)malloc(100000);
int i,n,c,j,c1;
int k=MAX_IN_LINE;
FILE *f;
char namefile[20];
  start1:;
  memset(path,'\0',100);
  memset(text,'\0',100000);
  cout<<"Import text from:";
  cout<<"\n 1. Keyboard";
  cout<<"\n 2. File \n";
  cin>>c1;
  if (c1==1)
  {
   cout <<"Please import text: \n";
   fflush(stdin);
   cin.getline(text, 100000);
  }
   else if (c1==2) 
      {  
		pathfile:;	
		cout<<"Import link to File text :";
		fflush(stdin);
		cin.getline(path,100);
		if ((f=fopen(path,"rt"))!=NULL)
		  while (feof(f) == 0)
		   {
		   fgets(line,1000,f);
		   strcat(text,line);
		   strcat(text," ");
		   memset(line,'\0',1000);
		   }
		else 
			{
			
			goto pathfile;
		    }
		fclose(f);
	  }
	else 
	 {
	  cout<<" Wrong press ! Please select again!!!\n";
	  goto start1;
	  }


system("cls"); 
start2:
cout << "Selection one operation : \n";
cout << " 1. Delete all the space in text. \n";
cout << " 2. Only delete the excess space in text. \n";
cout << " 3. Left Align \n";
cout << " 4. Right Align \n";
cout << " 5. Mid Align \n";
cout << "Press one number: ";
cin >>c;
system("cls");
cout<<"Text_result: \n\n";
if (c==1)
{
 	text=del_space(text);
 	cout<<text;
}
 else if (c==2)
 	{
 	text=del_excess_space(text);
 	cout<<text;
 }
 else if (c<=5)
  {
 	i=0;
 	text=del_excess_space(text);
 	n=strlen(text);
    memset(line,'\0',1000);
    while (i<n)
    { 
    if ((i+k)>n) 
    { 
       str_copy(line,text,i,n);
       if (c==3) left_align(line,MAX_IN_LINE);
       if (c==4) right_align(line,MAX_IN_LINE);
       if (c==5) mid_align(line,MAX_IN_LINE);
    }
    else
    {
	str_copy(line,text,i,i+k);
   	if (is_space(text[i+k])==1) 
   	 { 
   	   if (c==3) left_align(line,MAX_IN_LINE);
       if (c==4) right_align(line,MAX_IN_LINE);
       if (c==5) mid_align(line,MAX_IN_LINE);
   	  }
   	 else 
   	  for (j=k-1;j>0;j--) 
   	    if (is_space(line[j])==1) 
   	     {
   	     	memset(line,'\0',1000);
   	     	str_copy(line,text,i,i+j);
			if (c==3) left_align(line,MAX_IN_LINE);
            if (c==4) right_align(line,MAX_IN_LINE);
            if (c==5) mid_align(line,MAX_IN_LINE);
   	     	break;
   	     }
   	 }
   	i=i+strlen(line)+1;     
   	memset(line,'\0',1000);
     }
 
}
 else 
 {
  cout<<"Wrong press !!! Please select again !!! \n";
  goto start2;
 }
}
